package br.com.fogas.utils.dialog;

import br.com.fogas.utils.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;

public class DialogAlertConfirm extends DialogFragment {
	
	private int title, message, positiveButtonId, negativeButtonId;
	
	private DialogAlertConfirmListener listener;
	
	public static DialogAlertConfirm newInstance(DialogAlertConfirmListener listener) {
		return new DialogAlertConfirm().setListener(listener);
	}
	
	public DialogAlertConfirm setListener(DialogAlertConfirmListener listener) {
		this.listener = listener;
		return this;
	}

	public DialogAlertConfirm setTitle(int title) {
		this.title = title;
		return this;
	}
	
	public DialogAlertConfirm setMessage(int message) {
		this.message = message;
		return this;
	}
	
	public DialogAlertConfirm setPositiveButtonId(int positiveButtonId) {
		this.positiveButtonId = positiveButtonId;
		return this;
	}
	
	public DialogAlertConfirm setNegativeButtonId(int negativeButtonId) {
		this.negativeButtonId = negativeButtonId;
		return this;
	}
	
	public DialogAlertConfirm showDialog(FragmentManager fragmentManager) {
		show(fragmentManager, "DIALOG_ALERT");
		return this;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		if(positiveButtonId == 0) {
			positiveButtonId = R.string.confirmar;
		}
		if(negativeButtonId == 0) {
			negativeButtonId = R.string.cancelar;
		}
		
		Dialog dialog = new AlertDialog.Builder(getActivity())
		        .setTitle(title)
                .setMessage(message)		
		        .setPositiveButton(positiveButtonId, onPositiveClickListener)
				.setNegativeButton(negativeButtonId, onNegativeClickListener)
				.create();
		
		return dialog;
	}
	
	DialogInterface.OnClickListener onPositiveClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			listener.onPositiveClick(DialogAlertConfirm.this);
		}
	};
	
	DialogInterface.OnClickListener onNegativeClickListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			listener.onNegativeClick(DialogAlertConfirm.this);
		}
	};
	
}